import * as AntIcons from "@ant-design/icons";
import {Icon} from "@iconify/react";
import React from "react";
import classNames from "classnames";

export default function BaseIcon(props) {
    const AntIcon = AntIcons[props.iconName];
    const isCustom = props.iconName.startsWith("custom");
    const iconifyIcon = isCustom ? props.iconName.substring("custom-".length) : null;

    // 统一处理样式
    const baseStyle = {
        fontSize: props.fontSize || 16,
        color: props.color,
        cursor: props.onClick ? "pointer" : "default",
        ...props.style,
    };

    // 错误处理
    if (!iconifyIcon && !AntIcon) {
        return null; // 不渲染任何内容，避免显示错误信息占用空间
    }
    if (AntIcon) {
        return (
            <AntIcon
                className={classNames(props.className)}
                style={baseStyle}
                spin={false}
                onClick={props.onClick}
            />
        );
    }
    if (iconifyIcon) {
        return (
            <Icon
                icon={iconifyIcon}
                style={{
                    ...baseStyle,
                    width: props.fontSize || 20,
                    height: props.fontSize || 20,
                }}
                onClick={props.onClick}
            />
        );
    }
}
